package com.lee.mysql.controller;

import com.github.pagehelper.PageInfo;
import com.lee.mysql.pojo.Menu;
import com.lee.mysql.service.MenuService;
import com.lee.mysql.vo.MenuVo;
import com.lee.mysql.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;

/**
 * 菜单管理
 */

@RestController
@RequestMapping("/menu")
@Api(tags = {"菜单管理"})
public class MenuController {
    @Autowired
    private MenuService menuService;



    /** 新增菜单 */
    @RequiresPermissions("system:menu:add")
    @RequestMapping(value = "/addRecord", method = RequestMethod.POST)
    @ApiOperation(value = "新增菜单")
    public Result<String> addRecord(Menu record){
        if (StringUtils.isBlank(record.getName())
                || StringUtils.isBlank(record.getPmMark())){
            return new Result<>(Result.FAIL,"菜单名称/权限不能为空",null);
        }

        int i = menuService.addRecord(record);

        if (i > 0){
            return new Result<>(Result.SUCCESS,"添加成功",null);
        }else {
            return new Result<>(Result.FAIL,"添加失败",null);
        }
    }

    /** 根据ID修改记录 */
    @RequiresPermissions("system:menu:update")
    @RequestMapping(value = "/updateOneById", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID修改记录")
    public Result<String> updateOneById(Menu record){

        int i = menuService.updateOneById(record);

        if (i > 0){
            return new Result<>(Result.SUCCESS,"修改成功",null);
        }else {
            return new Result<>(Result.FAIL,"修改失败",null);
        }
    }

    /** 根据ID批量删除记录 */
    @RequiresPermissions("system:menu:delete")
    @RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID批量删除记录")
    @ApiImplicitParam(name = "ids", value = "id(单个,多个逗号隔开)", paramType = "query")
    public Result<String> deleteByIds(String ids){
        if (StringUtils.isBlank(ids)){
            return new Result<>(Result.FAIL,"请选择要删除的记录",null);
        }

        String[] split = ids.split(",");

        int i = menuService.deleteByIds(Arrays.asList(split));

        if (i > 0){
            return new Result<>(Result.SUCCESS,"删除成功",null);
        }else {
            return new Result<>(Result.FAIL,"删除失败",null);
        }
    }

    /** 根据ID查询记录 */
    @RequestMapping(value = "/queryOneById", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID查询记录")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query")
    public Result<Menu> queryOneById(String id){

        Menu record = menuService.queryOneById(id);

        return new Result<>(Result.SUCCESS,"操作成功",record);
    }


    /** 查询分页数据 */
    @RequiresPermissions("system:menu:view")
    @RequestMapping(value = "/queryPaging", method = RequestMethod.POST)
    @ApiOperation(value = "查询分页数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "页码", paramType = "query"),
        @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query")
    })
    public Result<PageInfo<Menu>> queryPaging(Menu record, Integer page, Integer size) {
        if (page == null) page = 1;
        if (size == null) size = 10;

        Result<PageInfo<Menu>> result = new Result<PageInfo<Menu>>();
        PageInfo<Menu> list = menuService.queryPaging(record, page, size);

        result.setData(list);
        result.setErrorCode(Result.SUCCESS);
        return result;
    }


    /** 查询所有菜单及子菜单 */
    @RequestMapping(value = "/queryMenuAndChild", method = RequestMethod.POST)
    @ApiOperation(value = "查询所有菜单及子菜单")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "type", value = "0只查询菜单 不传查所有", paramType = "query"),
        @ApiImplicitParam(name = "roleId", value = "角色ID(设置角色对应菜单选中)", paramType = "query")
    })
    public Result<List<MenuVo>> queryMenuAndChild(String type,String roleId) {
        List<MenuVo> list = menuService.queryMenuAndChild(type,roleId);
        return new Result<>(Result.SUCCESS,"操作成功",list);
    }






}
